Skip to content

Move LegacyBindingEnabled / JavascriptBindingPropertyName to per browser settings#5231

Open
campersau wants to merge 2 commits intocefsharp:masterfrom
campersau:perbrowserbindingname
Open

Move LegacyBindingEnabled / JavascriptBindingPropertyName to per browser settings#5231
campersau wants to merge 2 commits intocefsharp:masterfrom
campersau:perbrowserbindingname

Conversation

@campersau
Copy link
Copy Markdown
Contributor

@campersau campersau commented Apr 10, 2026

Fixes: Addresses #5229 (comment)

Summary:

  • JavascriptBindingPropertyName / LegacyBindingEnabled are now settings per browser instead of global ones.

Changes:

  • Moved LegacyBindingEnabled / JavascriptBindingPropertyName / JavascriptBindingPropertyNameCamelCase to JavascriptBindingSettings.h
  • Use GetOrAdd to update existing settings
  • Make IsJavascriptBindingApiAllowed static
  • Added test using multiple browsers
  • Added usermessage to Assert.True for better debugging

How Has This Been Tested?
Unittests

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Updated documentation

Checklist:

  • Tested the code(if applicable)
  • Commented my code
  • Changed the documentation(if applicable)
  • New files have a license disclaimer
  • The formatting is consistent with the project (project supports .editorconfig)

Summary by CodeRabbit

  • New Features

    • Support for custom global object names for JavaScript bindings per browser.
  • Improvements

    • Per-browser JavaScript binding settings with caching for more reliable multi‑browser behavior.
    • Legacy binding now follows per‑browser configuration.
  • Tests

    • Improved test diagnostics for script evaluations.
    • New test validating custom global names across multiple browsers.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 10, 2026

📝 Walkthrough

Walkthrough

JavaScript binding state was moved from wrapper-level fields to per-browser JavascriptBindingSettings cached via a factory; OnBrowserCreated/OnContextCreated now use per-browser settings; IsJavascriptBindingApiAllowed became static; three new settings properties were added and a multi-browser binding test was added.

Changes

Cohort / File(s) Summary
Wrapper changes
CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h, CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
Removed wrapper-level binding fields (_legacyBindingEnabled, _jsBindingPropertyName*), added static JavascriptBindingSettingsFactory, switched to _browserJavascriptBindingSettings->GetOrAdd(...) in OnBrowserCreated, and made IsJavascriptBindingApiAllowed static. OnContextCreated and binding flows now read from per-browser settings.
Per-browser settings
CefSharp.BrowserSubprocess.Core/JavascriptBindingSettings.h
Added properties: LegacyBindingEnabled (default false), JavascriptBindingPropertyName (default "CefSharp"), and JavascriptBindingPropertyNameCamelCase (default "cefSharp"). Constructor initializes these before existing API-related fields.
Tests
CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs
Improved assertion diagnostics to include evaluation messages and added ShouldWorkWhenUsingCustomGlobalObjectNameInMultipleBrowsers to validate custom global binding names across multiple browser instances.

Sequence Diagram

sequenceDiagram
    participant Browser as Browser Instance
    participant Cache as Per-Browser Cache
    participant Factory as Settings Factory
    participant Context as JS Context
    participant Binder as Binding Logic

    Browser->>Cache: OnBrowserCreated(browserId) -> GetOrAdd(browserId, factory)
    Cache->>Factory: invoke JavascriptBindingSettingsFactory(int)
    Factory-->>Cache: return JavascriptBindingSettings
    Cache-->>Browser: cached settings associated with browserId

    Browser->>Context: OnContextCreated(frame)
    Context->>Cache: TryGetValue(browserId)
    Cache-->>Context: JavascriptBindingSettings (or null)
    Context->>Binder: If settings != null -> evaluate settings (names, legacy flag)
    Binder-->>Context: Apply global property / legacy root bindings as per settings
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • GoutamSarkarHyland

Poem

🐰 A rabbit's little note:

Per-browser burrows snug and neat,
Each gets its own binding seat.
No shared names to hop or roam,
Now every context has a home—
Hooray for tidy JavaScript treats! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and concisely summarizes the main change: moving JavaScript binding configuration properties from global to per-browser settings.
Description check ✅ Passed The description follows the template with all required sections completed: fixes reference, summary, changes list, testing method, types of changes selected, and checklist items marked.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs (1)

91-122: Consider adding isolation assertions to verify per-browser settings don't leak.

The test verifies that each browser has its expected binding object, which is good. However, to fully validate per-browser isolation, consider also verifying that:

  • browser1 does not have window.cefSharp (since it has a custom name)
  • browser1 does not have window.bindingApiObject2
  • browser2 does not have window.bindingApiObject1

This would catch any accidental cross-contamination between browser instances sharing a render process.

💡 Example additional assertions
// After the existing assertions, add isolation checks:
var isolation1 = await browser1.EvaluateScriptAsync("typeof window.cefSharp === 'undefined'");
Assert.True(isolation1.Success, isolation1.Message);
Assert.True((bool)isolation1.Result, "browser1 should not have default cefSharp");

var isolation2 = await browser2.EvaluateScriptAsync("typeof window.cefSharp === 'undefined'");
Assert.True(isolation2.Success, isolation2.Message);
Assert.True((bool)isolation2.Result, "browser2 should not have default cefSharp");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs` around lines 91 -
122, The test ShouldWorkWhenUsingCustomGlobalObjectNameInMultipleBrowsers
currently checks that each browser exposes its expected global, but doesn't
assert that other globals are absent; add additional EvaluateScriptAsync checks
and assertions (using browser1, browser2, browser3 and Assert.True/Assert.False)
after the existing assertions to verify browser1 does not have window.cefSharp
or window.bindingApiObject2, browser2 does not have window.cefSharp or
window.bindingApiObject1, and browser3 does not have the custom names, ensuring
per-browser isolation and no render-process leakage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs`:
- Around line 91-122: The test
ShouldWorkWhenUsingCustomGlobalObjectNameInMultipleBrowsers currently checks
that each browser exposes its expected global, but doesn't assert that other
globals are absent; add additional EvaluateScriptAsync checks and assertions
(using browser1, browser2, browser3 and Assert.True/Assert.False) after the
existing assertions to verify browser1 does not have window.cefSharp or
window.bindingApiObject2, browser2 does not have window.cefSharp or
window.bindingApiObject1, and browser3 does not have the custom names, ensuring
per-browser isolation and no render-process leakage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4a007db3-794d-450b-997f-c0d3af1bb52d

📥 Commits

Reviewing files that changed from the base of the PR and between 00c8aa5 and 2d25f26.

📒 Files selected for processing (4)
  • CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
  • CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h
  • CefSharp.BrowserSubprocess.Core/JavascriptBindingSettings.h
  • CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs

@AppVeyorBot
Copy link
Copy Markdown

@AppVeyorBot
Copy link
Copy Markdown

@amaitland amaitland force-pushed the perbrowserbindingname branch from 1c471bb to 889332f Compare April 10, 2026 22:50
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs (1)

91-154: Well-structured test for per-browser binding isolation.

The test correctly validates that each browser instance maintains its own JavascriptBindingApiGlobalObjectName setting, ensuring the per-browser settings migration works as intended.

Optional: The sequential WaitForInitialLoadAsync calls could be parallelized for faster test execution:

await Task.WhenAll(
    browser1.WaitForInitialLoadAsync(),
    browser2.WaitForInitialLoadAsync(),
    browser3.WaitForInitialLoadAsync());
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs` around lines 91 -
154, The three sequential waits for initial load slow the test; replace the
three separate await browserX.WaitForInitialLoadAsync() calls with a single
parallel wait using Task.WhenAll to await browser1, browser2 and browser3
simultaneously (update the calls around WaitForInitialLoadAsync for those
browser variables).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs`:
- Around line 91-154: The three sequential waits for initial load slow the test;
replace the three separate await browserX.WaitForInitialLoadAsync() calls with a
single parallel wait using Task.WhenAll to await browser1, browser2 and browser3
simultaneously (update the calls around WaitForInitialLoadAsync for those
browser variables).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c8376f3b-00fa-460a-b799-3c3b16eb32d3

📥 Commits

Reviewing files that changed from the base of the PR and between 1c471bb and 889332f.

📒 Files selected for processing (4)
  • CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
  • CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h
  • CefSharp.BrowserSubprocess.Core/JavascriptBindingSettings.h
  • CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • CefSharp.BrowserSubprocess.Core/JavascriptBindingSettings.h

@AppVeyorBot
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants